<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>报价单信息</legend>
            <div style="margin: 3px 3px 3px 3px">
                <form class="layui-form layuimini-form" action="">
                    <input type="text" name="id" id="id" style="display: none"
                           class="layui-input">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">报价单名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="quotationName" id="quotationName"
                                       value="" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label required">报价单编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="quotationNo" id="quotationNo"
                                       lay-verify="required" value="" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label required">客人:</label>
                            <div class="layui-input-inline">
                                <select lay-verify="required" name="customerId" id="customerId" lay-search="">
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">报价时间</label>
                            <div class="layui-input-inline">
                                <input readonly="readonly" type="text" class="layui-input" name="quotationTime"
                                       id="quotationTime">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">币别</label>
                            <div class="layui-input-inline">
                                <input type="text" name="currencyCode" id="currencyCode"
                                       value="" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">FOR港口</label>
                            <div class="layui-input-inline">
                                <input type="text" name="harborFor" id="harborFor"
                                       value="" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-inline">
                                <input type="text" name="remark" id="remark"
                                       value="" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="save-quotation">
                                保存报价单
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                limit: 5,
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn isShow layui-btn-normal layui-btn-xs data-count-edit" lay-event="show">查看</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script src="/static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/static/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="/static/lib/jq-module/jquery.particleground.min.js" charset="utf-8"></script>
<script src="/static/js/common.js"></script>
<script>
    console.log("报价单")
    let quotationId = $.common.getUrlParam("quotationId");
    let customerId = $.common.getUrlParam("customerId");
    let contractId = $.common.getUrlParam("contractId");
    let isShow = $.common.getUrlParam("isShow");
    layui.use(['form', 'laydate', 'util'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            table = layui.table,
            layer = layui.layer;
        util = layui.util;
        var now = new Date();
        //初始化时间
        laydate.render({
            elem: '#quotationTime'
            , type: 'date',
            value: new Date()
        });
        //初始化下拉框 客人
        let callback = {
            onSuccess: function (result) {
                if (result.code == '0') {
                    $('#customerId').append(new Option('请选择', '', true));// 下拉菜单里添加元素
                    $.each(result.data, function (index, item) {
                        let option = new Option(item.customerName, item.id);
                        if (customerId) {
                            if (item.id == customerId) {
                                option.selected = true;
                            }
                        }
                        $('#customerId').append(option);// 下拉菜单里添加元素
                    });
                } else {
                    $.common.showErrorMessage(result.message)
                    return false;
                }
            }
        };
        $.xAjax('/customer/select', 'POST', JSON.stringify({}), callback);
        //合同下拉框
        let callback1 = {
            onSuccess: function (result) {
                if (result.code == '0') {
                    $('#contractId').append(new Option('请选择', '', true));// 下拉菜单里添加元素
                    $.each(result.data, function (index, item) {
                        let option = new Option(item.contractName, item.id);
                        if (contractId) {
                            if (item.id == contractId) {
                                option.selected = true;
                            }
                        }
                        $('#contractId').append(option);// 下拉菜单里添加元素
                    });
                } else {
                    $.common.showErrorMessage(result.message)
                    return false;
                }
            }
        };
        $.xAjax('/contract/select', 'POST', JSON.stringify({}), callback1);
        //初始化下拉框
        form.render("select");
        // 监听报价单提交
        form.on('submit(save-quotation)', function (data) {
            let url = "/quotation/saveOrUpdate"
            let callback = {
                onSuccess: function (result) {
                    if (result.code == '0') {
                     //   var iframeIndex = parent.layer.getFrameIndex(window.name);
                      //  parent.location.reload();
                    //    parent.layer.close(iframeIndex);
                        if(quotationId==null||quotationId==''){
                            quotationId = result.data.id
                        }
                        return ;

                    } else {
                        $.common.showErrorMessage(result.message)
                        return ;
                    }
                    return ;
                }
            };
            data.field.quotationTime = util.toDateString(data.field.quotationTime, 'yyyy-MM-dd HH:mm:ss');
            $.xAjax(url, 'POST', JSON.stringify(data.field), callback);
            return false;
        });
        let params = {};
        params.param = {"quotationId": quotationId};
        //列表
        table.render({
            elem: '#currentTableId',
            url: '/quotationDetail/pagesDetail',
            toolbar: '#toolbarDemo',
            method: "post",
            request: {
                pageName: "pageNum",
                limitName: "pageSize"
            },
            contentType: "application/json; charset=utf-8",
            where: params,
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'id', width: 65, title: 'ID', sort: true},
                {field: 'itemId', width: 100, title: '产品ID'},
                {field: 'itemName', width: 150, title: '产品名称'},
                {field: 'itemNo', width: 100, title: '产品编号'},
                {field: 'itemUnit', width: 100, title: '产品单位'},
                {field: 'itemWeight', width: 100, title: '产品重量'},
                {field: 'itemQuality', width: 100, title: '材质分析'},
                {field: 'customerItemsNo', width: 100, title: '客人货号'},
                {field: 'supplierItemsNo', width: 100, title: '工厂货号'},
                {field: 'customerPrice', width: 100, title: '客人价格'},
                {field: 'supplierPrice', width: 100, title: '"工厂价格'},
                {
                    field: 'itemSizeLength', width: 120, title: '产品尺寸-长', templet: function (a) {
                        if (a.itemSizeLength) {
                            return $.utils.lengthConversion(a.itemSizeLength);
                        }
                        return '';
                    }
                },
                {
                    field: 'itemSizeWidth', width: 120, title: '产品尺寸-宽', templet: function (a) {
                        if (a.itemSizeWidth) {
                            return $.utils.lengthConversion(a.itemSizeWidth);
                        }
                        return '';
                    }
                },
                {
                    field: 'itemSizeHeight', width: 120, title: '产品尺寸-高', templet: function (a) {
                        if (a.itemSizeHeight) {
                            return $.utils.lengthConversion(a.itemSizeHeight);
                        }
                        return '';
                    }
                },
                {field: 'innerBoxSizeLength', width: 120, title: '内箱-长cm'},
                {field: 'innerBoxSizeWidth', width: 120, title: '内箱-宽cm'},
                {field: 'innerBoxSizeHeight', width: 120, title: '内箱-高cm'},
                {field: 'boxSizeLength', width: 120, title: '外箱-长cm'},
                {field: 'boxSizeWidth', width: 120, title: '外箱-宽'},
                {field: 'boxSizeHeight', width: 120, title: '外箱-高'},
                {field: 'boxSroughWeight', width: 100, title: '外箱-毛重'},
                {field: 'boxSuttle', width: 100, title: '外箱-净重'},
                {field: 'cubeNumber', width: 100, title: '立方数'},
                {field: 'justCount', width: 100, title: '才数'},
                {field: 'supplierId', width: 80, title: '工厂ID'},
                {field: 'supplierNo', width: 120, title: '工厂编号'},
                {field: 'supplierName', width: 120, title: '工厂名称'},
                {field: 'packaging', width: 100, title: '包裝要求'},
                {field: 'innerBoxNumber', width: 80, title: '內盒数量'},
                {field: 'boxNumber', width: 120, title: '外箱数量'},
                {field: 'gw', width: 100, title: 'GW(KGS)'},
                {field: 'nw', width: 120, title: 'NW(KGS)'},
                {field: 'moq', width: 80, title: 'MOQ'},
                {field: 'remarks', width: 200, title: '备注'},
                {
                    field: 'createTime',
                    width: 120,
                    title: '创建时间',
                    templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
                },
                {field: 'updateUser', width: 100, title: '修改人'},
                {
                    field: 'updateTime',
                    width: 120,
                    title: '修改时间',
                    templet: "<div>{{layui.util.toDateString(d.updateTime, 'yyyy-MM-dd')}}</div>"
                },
                {
                    field: 'update',
                    title: '操作',
                    minWidth: 170,
                    toolbar: '#currentTableBar',
                    align: "center",
                    fixed: 'right'
                }
            ]],
            done: function (res, curr, count) {  //隐藏列
                if ($.common.userIsAdmin()) {
                    $("[data-field='updateUser']").css('display', 'none');
                    $("[data-field='updateTime']").css('display', 'none');
                }
                if (!$.common.userIsShowPower()) {
                    $("[data-field='customerPrice']").css('display', 'none');
                    $("[data-field='supplierPrice']").css('display', 'none');
                }
                if (isShow) {
                    $('.layui-btn:not(.isShow)').detach();
                }
            },
            limits: [5, 10, 15, 20, 25, 50, 100, 500],
            limit: 10,
            page: true
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                if (!quotationId || quotationId == '') {
                    $.common.showErrorMessage('请先保存报价单');
                    return false;
                }
                var index = layer.open({
                    title: '添加报价单明细',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/quotation/detail_add.html?quotationId=' + quotationId,
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'excelImport') {  // 导入
                var checkStatus = table.checkStatus('currentTableId');
                var data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });

        //监听
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'show') {
                var index = layer.open({
                    title: '查看报价单明细',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/quotation/detail_add.html?quotationId=' + quotationId
                        + '&itemId=' + data.itemId + '&supplierId=' + data.supplierId+"&isShow=true",
                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            //图片处理
                            if ('photo' == key && data[key]) {
                                let array = data[key].split(";");
                                for (let data in array) {
                                    let ph = array[data]
                                    if (ph) {
                                        body.find('#uploadImg').append('<img src="' + ph + '" class="layui-upload-img" width="50%" height="50%">');
                                    }
                                }
                            }
                            body.find("#" + key).val(data[key]);
                        }
                    },
                    end: function () {

                    }
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'edit') {
                var index = layer.open({
                    title: '修改报价单明细',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['95%', '95%'],
                    content: '/static/page/quotation/detail_add.html?quotationId=' + quotationId
                        + '&itemId=' + data.itemId + '&supplierId=' + data.supplierId,
                    data: data,
                    success: function (index, layero) {
                        var body = layer.getChildFrame('body', layero);
                        for (let key in data) {
                            //图片处理
                            if ('photo' == key && data[key]) {
                                let array = data[key].split(";");
                                for (let data in array) {
                                    let ph = array[data]
                                    if (ph) {
                                        body.find('#uploadImg').append('<img src="' + ph + '" class="layui-upload-img" width="50%" height="50%">');
                                    }
                                }
                            }
                            body.find("#" + key).val(data[key]);
                        }
                    },
                    end: function () {

                    }
                });
                lay(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }else if (obj.event === 'delete') {
                layer.confirm('确定删除？', function (index) {
                    let url = "/quotationDetail/delete/" + obj.data.id;
                    let callback = {
                        onSuccess: function (result) {
                            if (result.code == 0) {
                                window.location.reload();
                            } else {
                                $.common.showErrorMessage(result.message)
                                return false;
                            }
                        }
                    };
                    $.xAjax(url, 'POST', JSON.stringify(data.field), callback);
                });
            }
        });

    });
</script>
</body>
</html>
